package com.example.PolimiAssistant.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.PolimiAssistant.About;
import com.example.PolimiAssistant.LoginScreen;
import com.example.PolimiAssistant.Preferences;
import com.example.PolimiAssistant.R;
import com.example.PolimiAssistant.utils.AccountDataManager;
import com.example.PolimiAssistant.utils.PreferencesManager;

/**
 * Created by Vlado on 08.06.2014.
 * Common activity class from which all of our activity classes will inherit
 */
public class CommonActivity extends Activity{

    /**
     * Declare the context of the application
     */
    protected Context context = this;

    /**
     * The tag used for the logs.
     */
    protected final String TAG = context.getClass().getSimpleName();


    public void onCreate(Bundle savedInstanceState) {

        //1. Save the state of the activity
        super.onCreate(savedInstanceState);

        //2. Configure main display options

        //Avoid banding problems with the gradient background
        getWindow().setFormat(PixelFormat.RGBA_8888);

        //Set fixed portrait orientation
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //4. Extra configurations

        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // If your minSdkVersion is 11 or higher, instead use:
        getActionBar().setDisplayHomeAsUpEnabled(true);

        //5. Listeners
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_sync:
                //sync();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            case R.id.action_logout:
                logOut();
                return true;
            case R.id.action_about:
                openAbout();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * This method open the about activity
     */
    private void openAbout() {

        Intent goToAbout;

        goToAbout = new Intent(context, About.class);
        startActivity(goToAbout);

    }

    /**
     * This method open the settings activity
     */
    private void openSettings(){

        Intent goToSettings;

        goToSettings = new Intent(context, Preferences.class);
        startActivity(goToSettings);
    }

    /**
     * This method retrieves the information from the server and updates
     * all the student data in the phone
     */
    private void sync(){

    }

    /**
     * This method erase all of the user personal information stored in the
     * device and eliminate the user account from the android account
     * manager
     */
    public void logOut() {

        //1. Get the message strings
        String dialogMessage;
        dialogMessage = getResources().getString(R.string.logout_message_body);

        String dialogTitle;
        dialogTitle = getResources().getString(R.string.logout_message_title);

        String dialogYes;
        dialogYes = getResources().getString(R.string.logout_message_yes);

        String dialogNo;
        dialogNo = getResources().getString(R.string.logout_message_no);

        //2. Build and display the dialog
        DialogHandler dialog;
        dialog = new DialogHandler();
        dialog.confirmDialog(this, dialogTitle, dialogMessage, dialogYes, dialogNo,
                confirmlogOut(), denylogOut());



    }

    public Runnable confirmlogOut(){

        return new Runnable(){

            public void run(){

                //1. Get the username from the preferences
                PreferencesManager preferencesManager = new PreferencesManager(context);
                String username = preferencesManager.getUsername();

                //2. Delete the account using the account manager
                AccountDataManager.deleteAccount(context, username);

                //3. Create intent to go to the login screen
                Intent goToLogin;
                goToLogin = new Intent(context, LoginScreen.class);
                startActivity(goToLogin);

                //4. Inform the user
                Toast.makeText(context, "You have been logged out",
                        Toast.LENGTH_LONG).show();

                Log.i(TAG, "Logging out the user");
            }
        };
    }

    public Runnable denylogOut(){

        return new Runnable(){

            public void run(){
                Log.i(TAG,"The user rejected being logged out");
            }
        };
    }

    public void invalidTokenLogOut(){

        //1. Get the username from the preferences
        PreferencesManager preferencesManager = new PreferencesManager(context);
        String username = preferencesManager.getUsername();

        //2. Delete the account using the account manager
        AccountDataManager.deleteAccount(context, username);

        //3. Create intent to go to the login screen
        Intent goToLogin;
        goToLogin = new Intent(context, LoginScreen.class);
        startActivity(goToLogin);

        //4. Inform the user
        Toast.makeText(context, "Your session has expired, you will be logged out",
                Toast.LENGTH_LONG).show();

        Log.i(TAG, "Logging out the user");
    }
}
